import common.TreeNode;

import java.util.*;

public class levelOrder {
    public List<List<Integer>> levelOrder(TreeNode root){
        if(root == null) return List.of();
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> q = new ArrayDeque<>();
        q.offer(root);
        while(!q.isEmpty()){
            int sz = q.size();
            List<Integer> level = new ArrayList<>();
            for(int i = 0; i < sz; i++){
                TreeNode node = q.poll();
                level.add(node.val);
                if(node.left != null) q.offer(node.left);
                if(node.right != null) q.offer(node.right);
            }
            res.add(level);
        }
        return res;
    }

}
